<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/18
 * Time: 20:56
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class Comment
{
    public $comment_id = null;
    public function __construct($comment_id)
    {
        $this->comment_id = $comment_id;
    }

    public static function getComments($bowen_id){
        $comments = DB::table('weibo_bowen_comments');
        $result = $comments->where('comment_bowen_id','=',$bowen_id)->orderBy('comment_like_count', 'desc')->limit(7)->get();
        if($result){
            $data = [];
            for ($i = 0; $i < count($result); $i++){
                $author = new Author($result[$i]->comment_user_id);
                $data[$i] = [
                    'comment' => $result[$i],
                    'author' => $author->commentAuthor(),
                    'replys' => Reply::getReplys($result[$i]->comment_id)
                ];
            }
            return $data;
        }
        else{
            return null;
        }
    }

    public static function comBowen($dataArray){
        $comments = DB::table('weibo_bowen_comments');
        $result = $comments->insertGetId([
            'comment_content' => $dataArray['comment_content'],
            'comment_bowen_id' => $dataArray['comment_bowen_id'],
            'comment_user_id' => $dataArray['comment_user_id']
        ]);
        if($result){
            $comments = $comments->where('comment_id','=',$result)->get();
            if($comments){
                $data = [];
                $author = new Author($comments[0]->comment_user_id);
                $data[0] = [
                    'comment' => $comments[0],
                    'author' => $author->commentAuthor(),
                    'replys' => Reply::getReplys($comments[0]->comment_id)
                ];
                return $data;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }

    public function likeComment(){
        $comments = DB::table('weibo_bowen_comments');
        $result = $comments->where('comment_id','=',$this->comment_id)->increment('comment_like_count');
        if($result){
            $comments = $comments->where('comment_id','=',$this->comment_id)->select('comment_like_count')->get();
            return $comments[0];
        }
        else{
            return false;
        }
    }

    public static function getCommentsAdmin($data){
        $comment = DB::table('weibo_bowen_comments');
        $count = $comment->count();
        if(isset($data['orderBy']) && isset($data['order'])){
            if($data['orderBy'] == 'id'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_id','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_id','asc');
                }
            }
            else if($data['orderBy'] == 'reply'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_reply_count','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_reply_count','asc');
                }
            }
            else if($data['orderBy'] == 'like'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_like_count','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_like_count','asc');
                }
            }
            else if($data['orderBy'] == 'time'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_posttime','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_posttime','asc');
                }
            }
            else if($data['orderBy'] == 'bowen'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_bowen_id','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_bowen_id','asc');
                }
            }
            else if($data['orderBy'] == 'author'){
                if($data['order'] == 'desc'){
                    $comment = $comment -> orderBy('comment_user_id','desc');
                }
                else{
                    $comment = $comment -> orderBy('comment_user_id','asc');
                }
            }
            else{
                $comment = $comment -> orderBy('comment_id','asc');
            }
        }
        else{
            $comment = $comment -> orderBy('comment_id','asc');
        }

        if(isset($data['index']) && isset($data['limit'])){
            $comment -> skip(($data['index'] - 1) * $data['limit']) -> take(20);
        }
        else{
            $comment -> skip(1) -> take(20);
        }

        $comment = $comment -> get();

        if($comment){
            $result = [];
            $result['total'] = $count;
            $result['comments'] = $comment;
            return $result;
        }
        else{
            return null;
        }
    }
}